var setting = {
	view: {
		showLine: false,
		showIcon: false,
		selectedMulti: false,
		dblClickExpand: false
	},
	data: {
		simpleData: {
			enable: true
		}
	},
	callback: {
		beforeExpand: beforeExpand,
		onExpand: onExpand,
		onClick: onClick
	}
};

/**
 * 初始化IFrame 数据
 */
function initIFrame() {
	var iframe = document.getElementById("mainFrame");
	var leftDiv = document.getElementById("contentDiv_left");
	var contentDiv_right = document.getElementById("contentDiv_right");
	var contentDiv = document.getElementById("contentDiv");
	var scrollModel = document.getElementById("contentDiv_left_scrollModel_content");
	var height=741;
	iframe.height =  height;
	if(isIE6Version()) {
		contentDiv_right.style.height =  (height + 33) + "px";
	}else {
		contentDiv_right.style.height = height + "px";
		contentDiv.style.height = height + "px";
	}
	leftDiv.style.height = (height ) + "px";
	scrollModel.style.height = (height - 249) + "px";
	
	/*
	iframe.height = "741px";
	iframe.contentWindow.document.body.scrollHeight = "741px";
	iframe.contentWindow.document.documentElement.scrollHeight = "741px";
	leftDiv.style.height = "492px";
	scrollModel.style.height = "462px";
	contentDiv_right.style.height = "741px";
	contentDiv.style.height = "741px";
	*/
}


function onClick(event, treeId, treeNode, clickFlag) {
	initIFrame();
	if(treeNode.name == '华南交通工程技术' || treeNode.name == '著作' || treeNode.name == '成果简介') {
		singlePath(treeNode);
	}
	
	if(treeNode.isParent ){
		var zTree = $.fn.zTree.getZTreeObj("tree3");
		zTree.expandNode(treeNode, null, null, null, true);
	}else{
		/*
		var url = "sysmanage/modules.action?cmd=search&po.id="+treeNode.id
		if(treeNode.title=='CommonLink'){
			return ;
		}
		$.ajax({
			type: "GET",
			url: url,
			dataType: "html",
			success: function(returnedData){
				$$("searchConditionDiv").innerHTML = returnedData;
			}
		});
		*/
		var parentNode = treeNode.getParentNode();
		var url = "common/CommLinkAction.action?cmd=treeNodeName";
		$.ajax({
			type: "post",
			url: url,
			data:{
				'param':parentNode.name
			},
			dataType: "html",
			success: function(returnedData){
				//$$("searchConditionDiv").innerHTML = returnedData;
			}
		});
	}
	
}		

	var curExpandNode = null;
		function beforeExpand(treeId, treeNode) {
			var pNode = curExpandNode ? curExpandNode.getParentNode():null;
			var treeNodeP = treeNode.parentTId ? treeNode.getParentNode():null;
			var zTree = $.fn.zTree.getZTreeObj("tree3");
			for(var i=0, l=!treeNodeP ? 0:treeNodeP.children.length; i<l; i++ ) {
				if (treeNode !== treeNodeP.children[i]) {
					zTree.expandNode(treeNodeP.children[i], false);
				}
			}
			while (pNode) {
				if (pNode === treeNode) {
					break;
				}
				pNode = pNode.getParentNode();
			}
			if (!pNode) {
				singlePath(treeNode);
			}

		}
		function singlePath(newNode) {
			if (newNode === curExpandNode) return;
			if (curExpandNode && curExpandNode.open==true) {
				var zTree = $.fn.zTree.getZTreeObj("tree3");
				if (newNode.parentTId === curExpandNode.parentTId) {
					zTree.expandNode(curExpandNode, false);
				} else {
					var newParents = [];
					while (newNode) {
						newNode = newNode.getParentNode();
						if (newNode === curExpandNode) {
							newParents = null;
							break;
						} else if (newNode) {
							newParents.push(newNode);
						}
					}
					if (newParents!=null) {
						var oldNode = curExpandNode;
						var oldParents = [];
						while (oldNode) {
							oldNode = oldNode.getParentNode();
							if (oldNode) {
								oldParents.push(oldNode);
							}
						}
						if (newParents.length>0) {
							for (var i = Math.min(newParents.length, oldParents.length)-1; i>=0; i--) {
								if (newParents[i] !== oldParents[i]) {
									zTree.expandNode(oldParents[i], false);
									break;
								}
							}
						} else {
							zTree.expandNode(oldParents[oldParents.length-1], false);
						}
					}
				}
			}
			curExpandNode = newNode;
		}
		
		
		function onExpand(event, treeId, treeNode) {
			curExpandNode = treeNode;
		}

function initMenu(){
	var url = "sysmanage/modules.action?cmd=menu";
	$.ajax({
		type: "POST",
		url: url,
		dataType: "json",
		success: function(returnedData){
			var data = returnedData.ZHANGLI;
			$.fn.zTree.init($("#tree3"), setting, data);
		},
		error : function(XMLHttpRequest, textStatus, errorThrown){
			var data = $.evalJSON(XMLHttpRequest.responseText).ZHANGLI;
			$.fn.zTree.init($("#tree3"), setting, data);
		}
	});
}

function addDiyDom(treeId, treeNode) {
	var spaceWidth = 8;
	var switchObj = $("#" + treeNode.tId + "_switch"),
	icoObj = $("#" + treeNode.tId + "_ico");
	switchObj.remove();
	icoObj.before(switchObj);
	if (treeNode.level > 1) {
		var spaceStr = "<span style='display: inline-block;width:" + (spaceWidth * treeNode.level)+ "px'></span>";
		switchObj.before(spaceStr);
	}
}


function search(){
	initIFrame();
	var tableName = $$("tableNameSel").value;
	var searchProperty = $$("searchPropertySel").value;
	var searchKey = $$("searchKey").value;
	if(searchProperty==-1){
		alert("请选择搜索属性");
		return;
	}
	if(searchKey.trim()==''){
		alert("请填写搜索属性值");
		return;
	}
	var s = tableName.indexOf("|");
	if(s==-1){
		alert("请选择表名");
		return;
	}
	var p = searchProperty.indexOf("_sel");
	if(p != -1){
		searchProperty = searchProperty.substring(0,p);
	}
	
	var d = searchProperty.indexOf("_date_");
	if(d != -1){
		var format = searchProperty.substring(d+6);
		if(!IsDateFormat($$("searchKey"),"关健字",format)){
			return ;
		}
		searchProperty = searchProperty.substring(0,d);
	}
	var modulesId = tableName.substring(0,s);
	var url = tableName.substring(1+s);
	var ps = url.indexOf("?");
	if(ps!=-1){
		url = url.substring(0,ps);
	}
	searchKey = encodeURIComponent(searchKey);
	url = url + "?cmd=goAllSearch&formMap.modulesId="+modulesId
		+"&formMap.searchProperty="+searchProperty+"&formMap.searchKey="+searchKey;
	mainFrame.location.href=url;
	
}